﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTD2013.DTDLib;
using DTD2013.Common;
using System.Text.RegularExpressions;

namespace DTD2013.MainApp.UI
{
    public partial class guiExpressionEditor : XtraForm
    {
        #region Properties

        public string Expression
        {
            get { return medExpression.Text; }
            set { medExpression.Text = value; }
        }

        #endregion
        public guiExpressionEditor()
        {
            InitializeComponent();

            medExpression.KeyPress += new KeyPressEventHandler(medExpression_KeyPress);
            lbxCode.Click += new EventHandler(lbxCode_Click);
            lbxCode.DoubleClick += new EventHandler(lbxCode_DoubleClick);
        }
        
        public guiExpressionEditor(string expression, List<string> codeList) : this()
        {
            medExpression.Text = expression;
            lbxCode.Items.AddRange(codeList.ToArray());
        }

        private void medExpression_KeyPress(object sender, KeyPressEventArgs e)
        {            
            if (e.KeyChar == 8 || e.KeyChar == '+' || e.KeyChar == '-' || e.KeyChar == '*' ||
                e.KeyChar == '/' || e.KeyChar == '%' || e.KeyChar == '(' || e.KeyChar == ')')
            {
                e.Handled = false;
            }
            else if (e.KeyChar < 48 || e.KeyChar > 57)
            {
                e.Handled = true;
            }
        }

        private void lbxCode_Click(object sender, EventArgs e)
        {
            lblDescription.Text = DataUtilities.GetSymbolDescription(lbxCode.SelectedItem.ToString());
        }

        private void lbxCode_DoubleClick(object sender, EventArgs e)
        {
            int selectionStart = medExpression.SelectionStart;
            medExpression.Text = medExpression.Text.Insert(selectionStart, lbxCode.SelectedItem.ToString());
            medExpression.SelectionStart = selectionStart + lbxCode.SelectedItem.ToString().Length;
        }
        
        private void btnOK_Click(object sender, EventArgs e)
        {
            string expression = medExpression.Text;
            string pattern = @"\[.*?\]";
            Regex re = new Regex(pattern);
            MatchCollection mc = re.Matches(expression);
            foreach (Match m in mc)
            {
                expression = expression.Replace(m.Value, Constant.ZERO);
            }
            try
            {
                DataUtilities.CalculateExpression(expression);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch(Exception)
            {
                XtraMessageBox.Show(MessageResource.EXPRESSION_ERROR, MessageResource.ERROR, 
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
